package com.babystudy.dao;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.ResultSetHandler;
import org.apache.commons.dbutils.handlers.BeanListHandler;
import org.springframework.stereotype.Service;

import com.babystudy.pojo.User;
import com.db.utils.DBUtilsHelper;



public class UserDaoImpl implements UserDao {
	private static DBUtilsHelper dbh;
	
	public int saveDownload(User user){
		dbh = new DBUtilsHelper(); 
		 QueryRunner runner = dbh.getRunner(); 
			try{
				 			
			    // System.out.println("NonWorkLoadDaoImpl----insertWorkLoadBywuid---pjdesc"+wuid);
				
					runner.update("insert into user(uid,download_time,download_content,ip_addr) "
							+ "values(?,?,?,?)",user.getUid(),user.getDownload_time(),user.getDownload_content(),user.getIp_addr());
					return 1;
				} catch (Exception e) {
					System.out.println("error......");
					e.printStackTrace();
					return 0;
				}
	}
	
	public ArrayList<User> getAllUsers(){
	
		dbh = new DBUtilsHelper(); 
		 QueryRunner runner = dbh.getRunner(); 
		 ResultSetHandler<List<User>> h = new BeanListHandler<User>(User.class);
		 ArrayList<User> userList;
		try {
			userList = (ArrayList<User>)runner.query("select * from user",h);
			
			return userList;
		} catch (SQLException e) {
			e.printStackTrace();
			return null;
		}
}
}
